﻿CREATE PROCEDURE [dbo].[yaf_forum_updatelastpost](
                @ForumID INT)
AS
    BEGIN
        UPDATE yaf_Forum
        SET    LastPosted = (SELECT   TOP 1 y.Posted
                             FROM     yaf_Topic x,
                                      yaf_Message y
                             WHERE    x.ForumID = yaf_Forum.ForumID
                             AND y.TopicID = x.TopicID
                             AND (y.Flags & 24) = 16
                             ORDER BY y.Posted DESC),
               LastTopicID = (SELECT   TOP 1 y.TopicID
                              FROM     yaf_Topic x,
                                       yaf_Message y
                              WHERE    x.ForumID = yaf_Forum.ForumID
                              AND y.TopicID = x.TopicID
                              AND (y.Flags & 24) = 16
                              ORDER BY y.Posted DESC),
               LastMessageID = (SELECT   TOP 1 y.MessageID
                                FROM     yaf_Topic x,
                                         yaf_Message y
                                WHERE    x.ForumID = yaf_Forum.ForumID
                                AND y.TopicID = x.TopicID
                                AND (y.Flags & 24) = 16
                                ORDER BY y.Posted DESC),
               LastUserID = (SELECT   TOP 1 y.UserID
                             FROM     yaf_Topic x,
                                      yaf_Message y
                             WHERE    x.ForumID = yaf_Forum.ForumID
                             AND y.TopicID = x.TopicID
                             AND (y.Flags & 24) = 16
                             ORDER BY y.Posted DESC),
               LastUserName = (SELECT   TOP 1 y.UserName
                               FROM     yaf_Topic x,
                                        yaf_Message y
                               WHERE    x.ForumID = yaf_Forum.ForumID
                               AND y.TopicID = x.TopicID
                               AND (y.Flags & 24) = 16
                               ORDER BY y.Posted DESC)
        WHERE  ForumID = @ForumID
    END


